//
//  RecordViewController.m
//  51-Tour
//
//  Created by xiang ying on 14-3-20.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "RecordViewController.h"
#import "ServiceManage.h"
#import "SVProgressHUD.h"
#import "NSString+TPCategory.h"

@interface RecordViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView     *_recordList;
    NSArray         *_recordArray;
}

@end

@implementation RecordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationItme:[[DataCenter instance] getStringForKey:@"Record"]];
    
    _recordList = [[UITableView alloc] initWithFrame:self.contentView.bounds];
    _recordList.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_recordList setBackgroundColor:[UIColor clearColor]];
    _recordList.dataSource = self;
    _recordList.delegate = self;
    [self.contentView addSubview:_recordList];
    
    [SVProgressHUD showWithStatus:[[DataCenter instance] getStringForKey:@"loading"]];
    //加载讲解记录
    
    [ServiceManage getUserlogRequestInfo:@{@"Mobile": [DataCenter instance].user.userPhone,@"LanKey":[[DataCenter instance] getLanguageType]==zh_cn?@"zh-cn":@"en-us"} completion:^(RespondDTO *respond) {
        if (respond.flag == respond_success) {
            _recordArray = respond.data;
            [_recordList reloadData];
            [SVProgressHUD dismiss];
//            [SVProgressHUD showSuccessWithStatus:respond.msg];
        }else{
            [SVProgressHUD showErrorWithStatus:TIP_ERROR];
            [self performSelector:@selector(popself) withObject:nil afterDelay:0.5];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)popself{
    [self.navigationController popViewControllerAnimated:NO];
    [[UIManagement shareInstance] showRight];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _recordArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor clearColor];
        
        
        UIImageView *_icon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 11, 50)];
        _icon.tag = 11;
        [cell addSubview:_icon];
        
        
        UILabel *_timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, 280, 30)];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.tag = 22;
        _timeLabel.font = [UIFont systemFontOfSize:14];
        _timeLabel.textColor = THEMECOLOR;
        [cell addSubview:_timeLabel];
        
        UILabel *_contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_timeLabel.frame.origin.x, _timeLabel.frame.size.height, 290, 17)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.tag = 33;
        _contentLabel.textColor = [UIColor darkGrayColor];
        _contentLabel.font = [UIFont systemFontOfSize:13];
        [cell addSubview:_contentLabel];
        
        UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
        _line.tag = 44;
        _line.frame = CGRectMake(30, cell.frame.size.height-1, 290, 1);
        [cell addSubview:_line];
    }
    
    UIImageView *_icon = (UIImageView*)[cell viewWithTag:11];
    UIImageView *_line = (UIImageView*)[cell viewWithTag:44];
    UILabel     *_timeLabel = (UILabel*)[cell viewWithTag:22];
    UILabel     *_contentLabel = (UILabel*)[cell viewWithTag:33];

    if (indexPath.row == 0) {
        _icon.image = [[UIImage imageNamed:@"time-base1_07"] stretchableImageWithLeftCapWidth:0 topCapHeight:60];
    }
    else if (indexPath.row == _recordArray.count-1){
        _icon.image = [[UIImage imageNamed:@"time-base3_07"]stretchableImageWithLeftCapWidth:0 topCapHeight:60];
    }
    else{
        _icon.image = [[UIImage imageNamed:@"time-base2_07"]stretchableImageWithLeftCapWidth:0 topCapHeight:60];
    }
    RecordDTO *dto = _recordArray[indexPath.row];
    
    _timeLabel.text = dto.time;
    
    CGFloat height = [dto.description heightwithfont:_contentLabel.font width:_contentLabel.frame.size.width];
    _contentLabel.numberOfLines = height/13+1;
    _contentLabel.frame = CGRectMake(_contentLabel.frame.origin.x, _contentLabel.frame.origin.y, _contentLabel.frame.size.width, height);
    _contentLabel.text = dto.description;
    _icon.frame = CGRectMake(_icon.frame.origin.x, _icon.frame.origin.y, _icon.frame.size.width, _contentLabel.frame.size.height+30+5);
    _line.frame = CGRectMake(_line.frame.origin.x, _icon.frame.origin.y+_icon.frame.size.height-1, _line.frame.size.width, 1);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecordDTO *dto = _recordArray[indexPath.row];
    CGFloat height = [dto.description heightwithfont:[UIFont systemFontOfSize:13] width:290];

    return height+30+5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

@end
